<?php defined('IN_LK') or die('Access Denied');
lk::lo('model', '', 0);
class member_model extends model
{
	public function __construct()
	{
		$this->tablename = 'member';
		parent::__construct();
	}
	
	/**
	 * 检查用户名密码是否正确
	 * @param string $username 用户名
	 * @param string $password 原始密码
	 * @param string $client   提示错误
	 * @return string 验证成功返回加密后的密码
	 */
	public function login_check($username, $password, $client = 0)
	{
		if(!is_username($username))
		{
			if(!$client)
			{
				showmsg(LANG('username_illegal'), false);
			}
			else
			{
				return false;
			}
		}
		if(!is_password($password))
		{
			if(!$client)
			{
				showmsg(LANG('password_illegal2', 'member'), false);
			}
			else
			{
				return false;
			}
		}
		return password($password);
	}
	
	/**
	 * 检查某个字段是否存在重复
	 * @param  string $field   字段名
	 * @param  string $value   不允许重复的值
	 * @param  int    $allowid 允许重复的id（自己）
	 * @return bool   $r       存在返回true
	 */
	public function is_exists($field, $value, $allowid = 0)
	{
		$where  = "`$field`='$value'";
		$where .= $allowid ? " AND `userid`!='$allowid'" : '';
		return (boolean)$this->get_one('userid', $where);
	}
	
	/**
	 * 登录动作
	 * @param string $username 用户名
	 * @param string $password 加密后的密码
	 * @return 登录成功注册session，cookie 失败返回false
	 */
	public function dologin($username, $password, $cookietime = 0, $client = 0)
	{
		$r = $this->get_one('userid,groupid,islock', "`username`='$username' AND `password`='$password'");
		if(!$r)
		{
			if($client)
			{
				return false;
			}
			showmsg(LANG('user_pwd_illegal'), false);
		}
		if($r && $r['islock'])
		{
			if($client)
			{
				return false;
			}
			showmsg(LANG('user_locked'), false);
		}
		if($cookietime)
		{
			$authkey = md5(AUTH_KEY . $_SERVER['HTTP_USER_AGENT']);
			$params  = $r['userid'] . "|" . $r['groupid'] . "|" . $username . '|' . $password;
			$content = lk_auth($params, 'ENCODE', $authkey);
			set_cookie('_lkauth', $content, TIME + $cookietime);
		}
		else
		{
			set_cookie('_lkauth', '');
		}
		$_SESSION['_userid']  = $r['userid'];
		$_SESSION['_groupid'] = $r['groupid'];
		$_SESSION['_username']= $username;
		$update = array('lastlogintime' => TIME, 
						'lastloginip'   => get_ip(),
						'logintimes'    => '+=1');
		$this->update($update, "`userid`='" . $r['userid'] . "'");  //更新member
	}
	
	/**
	 * 注册动作
	 * @param array $info 注册所需数组 必须含 username,password1,password2,nickname,email
	 */
	public function doregister($info)
	{
		if(!is_password($info['password1']) || $info['password1'] != $info['password2'])
		{
			showmsg(LANG('password_illegal', 'member'), false);
		}
		if(!is_username($info['username']) || $this->is_exists('username', $info['username']))
		{
			showmsg(LANG('username_illegal', 'member'), false);
		}
		if(!is_email($info['email']) || $this->is_exists('email', $info['email']))
		{
			showmsg(LANG('email_illegal', 'member'), false);
		}
		if(!is_username($info['nickname']) || $this->is_exists('nickname', $info['nickname']))
		{
			showmsg(LANG('nickname_illegal', 'member'), false);
		}
		$info['password'] = password($info['password1']);
		$info['regtime']  = TIME;
		$info['regip']    = get_ip();
		$info['groupid']  = 2;  //注册会员
		unset($info['password1'], $info['password2']);
		$r_i = $this->insert($info);
		$this->group_db  = lk::load_model('member_group');
		$r_u = $this->group_db->update(array('totalnum'=>'+=1'), "`groupid`='2'");
		if($r_i && $r_u)
		{
			$this->dologin($info['username'], $info['password']);
			return true;
		}
		return false;
	}
}